package cn.leida.ruoyi.system.dao;


import cn.leida.ruoyi.common.core.domain.entity.SysMenu;
import cn.leida.ruoyi.system.dao.custom.SysMenuDaoCustom;
import com.alibaba.fastjson2.JSONObject;
import org.hibernate.query.sql.internal.NativeQueryImpl;
import org.hibernate.transform.Transformers;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单表 数据层
 *
 * @author liutietou
 * @since 1.0  2020-12-11
 */
@Repository
public interface SysMenuDao extends JpaRepository<SysMenu, Long>, JpaSpecificationExecutor<SysMenu>, SysMenuDaoCustom {

    @Query(value = " select count(1) from sys_menu where parent_id =?1   ", nativeQuery = true)
    Integer hasChildByMenuId(Long menuId);

    List<SysMenu> findByMenuNameAndParentId(String menuName, Long parentId);

    @Modifying
    @Query(value = " delete from sys_menu where menu_id >= ?1  ", nativeQuery = true)
    Integer deleteByMinMenuId(Long minMenuId);

    @Query(value = """
          select m.* from sys_menu m
          left join sys_role_menu rm on m.menu_id = rm.menu_id
          left join sys_user_role ur on rm.role_id = ur.role_id
          left join sys_role ro on ur.role_id = ro.role_id
          where ur.user_id = ?1 and m.visible = ?2 and m.status = ?3
          order by m.parent_id, m.order_num
        """, nativeQuery = true)
    List<JSONObject> queryUserMenuList(Long userId, String visible, String status);


    @Query(value = """
          select m.* from sys_menu m 
          where m.menu_type in ('M', 'C') and m.status = 0
          order by m.parent_id, m.order_num
        """, nativeQuery = true)
    List<JSONObject> findMenuTreeAllList();

    @Query(value = """
          select distinct m.perms as perms  from sys_menu m 
          left join sys_role_menu rm on m.menu_id = rm.menu_id
         left join sys_user_role ur on rm.role_id = ur.role_id 
         left join sys_role r on r.role_id = ur.role_id
         where m.status = '0' and r.status = '0' and ur.user_id = ?
        """, nativeQuery = true)
    List<String> findMenuPermsByUserId(Long userId);

    @Query(value = """
          select m.* from sys_menu m
          left join sys_role_menu rm on m.menu_id = rm.menu_id
          left join sys_user_role ur on rm.role_id = ur.role_id
          left join sys_role ro on ur.role_id = ro.role_id
          left join sys_user u on ur.user_id = u.user_id
          where u.user_id = ?1 and m.menu_type in ('M', 'C') and m.status = 0  AND ro.status = 0
          order by m.parent_id, m.order_num 
        """, nativeQuery = true)
    List<JSONObject> findMenuTreeByUserId(Long userId);

    @Query(value = """
         select m.menuId  from SysMenu m left join SysRoleMenu rm on m.menuId = rm.menuId
         where rm.roleId = ?1  order by m.parentId, m.orderNum 
        """)
    public List<Long> findMenuListByRoleId(Long roleId);

    @Query(value = """
           from SysMenu m left join SysRoleMenu rm on m.menuId = rm.menuId
         where rm.roleId = ?1  order by m.parentId, m.orderNum 
        """)
    public List<SysMenu> findRoleMenuList(Long roleId);

    @Query(value = """
         select m.menuId  from SysMenu m left join SysRoleMenu rm on m.menuId = rm.menuId
         where rm.roleId = ?1  and m.menuId not in (select m.parentId from SysMenu m inner join SysRoleMenu rm on m.menuId = rm.menuId and rm.roleId = ?1 )
         order by m.parentId, m.orderNum 
        """)
    public List<Long> findMenuCheckListByRoleId(Long roleId);

}
